package com.exercise.memberscheduling.application;

import com.exercise.memberscheduling.domain.entity.MemberEntity;
import com.exercise.memberscheduling.domain.valueObject.Member;
import com.exercise.memberscheduling.domain.valueObject.MemberOperate;
import com.exercise.memberscheduling.frequency.LimitRequest;
import com.exercise.memberscheduling.tools.ExportExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("/memberScheduling")
public class MemberController {
    @Autowired
    private MemberEntity memberEntity;
    @Autowired
    private ExportExcelUtils exportExcelUtils;
    @RequestMapping("")
    public String index()
    {
        return "index";
    }
    @RequestMapping("/user")
    public String userInfo(Model model)
    {
        model.addAttribute("users",memberEntity.selAllMembers());
        return "user";
    }
    @RequestMapping("/changePassword")
    public String changePassword()
    {
        return "updatePwd";
    }
    //更改管理密码
    @RequestMapping("/updatePassword")
    public String updatePassword(@RequestParam("newPassword")String newPassword)
    {
        return "redirect:/memberScheduling/user";
    }
    @RequestMapping("/AllHistoryOperate")
    public String AllHistoryOperate(Model model)
    {
        model.addAttribute("AllHistoryOperates",memberEntity.selAllOperate());
        return "AllHistoryOperate";
    }
    @LimitRequest
    @PostMapping("/addUser")
    public String addMember(@RequestParam("MemberName") String memberName)
    {
        if(memberName==null||memberName.equals("")){return "redirect:/memberScheduling/user";}
        Member member = new Member();
        member.setMemberName(memberName);
        memberEntity.addMember(member);
        return "redirect:/memberScheduling/user";
    }
    @LimitRequest
    @PostMapping("/BeginClass/{memberId}")
    public String BeginClass( @PathVariable("memberId") String memberId)
    {
        memberEntity.BeginClass(Integer.parseInt(memberId));
        return "redirect:/memberScheduling/user";
    }
    @LimitRequest
    @PostMapping("/BuyCourseTimes/{memberId}")
    public String BuyCourseTimes(@RequestParam("courseTimes") String courseTimes, @PathVariable String memberId)
    {
        if(courseTimes==null||courseTimes.equals(""))
        {
            return "redirect:/memberScheduling/user";
        }
        Member member = new Member();
        courseTimes = courseTimes.trim();
        memberEntity.BuyCourseTimes(Integer.parseInt(memberId),Integer.parseInt(courseTimes));
        return "redirect:/memberScheduling/user";
    }
    @PostMapping("/changeMemberName/{memberId}")
    public String changeMemberName(@RequestParam("memberName") String memberName, @PathVariable String memberId)
    {
        if(memberName==null||memberName.equals(""))
        {
            return "redirect:/memberScheduling/user";
        }
        memberEntity.changeMemberName(Integer.parseInt(memberId),memberName);
        return "redirect:/memberScheduling/user";
    }
    @PostMapping("/changeMemberCourseTimes/{memberId}")
    public String changeMemberCourseTimes(@PathVariable String memberId,@RequestParam("changedCourseTimes") String changedCourseTimes)
    {
        if(changedCourseTimes==null||changedCourseTimes.equals(""))
        {
            return "redirect:/memberScheduling/user";
        }
        memberEntity.changeCoursesTimes(Integer.parseInt(memberId),Integer.parseInt(changedCourseTimes));
        return "redirect:/memberScheduling/user";
    }
    //修改管理员密码
    @PostMapping("/changeAdminPassword")
    public String changeAdminPassword(@RequestParam("newPassword")String newPassword)
    {
        if(newPassword==null||newPassword.equals(""))
        {
            return "redirect:/memberScheduling/user";
        }
        memberEntity.reviseAdminPassword(newPassword);
        return "redirect:/memberScheduling/user";
    }
    //模糊查询一个会员信息
    @GetMapping("/searchOneUser")
    public String searchOneUser(@RequestParam("memberName")String memberName,Model model)
    {
        if(memberName==null||memberName.equals("")||memberEntity.selMemberByName(memberName)==null)
        {
            return "redirect:/memberScheduling/user";
        }
        model.addAttribute("users",memberEntity.selMemberByName(memberName));
        return "searchUser";
    }
    //查询一个会员的历史操作记录
    @GetMapping("/searchUserOperate")
    public String searchOneUserOperate(@RequestParam("memberName")String memberName,Model model)
    {
        if(memberName==null||memberName.equals("")||memberEntity.selMemberByName(memberName)==null)
        {
            return "redirect:/memberScheduling/AllHistoryOperate";
        }
        Member member = new Member();
        member.setMemberName(memberName);
        model.addAttribute("AllHistoryOperates",memberEntity.selOperateByMemberName(member));
        return "searchUserOperate";
    }
    //删除一个会员
    @PostMapping("/deleteMember/{memberId}")
    public String delMember(@PathVariable String memberId)
    {
        memberEntity.delMember(memberId);
        return "redirect:/memberScheduling/user";
    }
    //查询一段时间内的上课数
    @GetMapping("/searchPeriodBeginCourses")
    public String searchPeriodBeginCourses(@RequestParam("startTime")String startTime,@RequestParam("endTime")String endTime,Model model)
    {
        List<Object> sums = memberEntity.periodBeginsCourses(startTime,endTime);
        model.addAttribute("startTime",startTime);
        model.addAttribute("endTime",endTime);
        model.addAttribute("beginCourseTimes",(int)sums.get(0));
        model.addAttribute("buyCourseTimes",(int)sums.get(1));
        model.addAttribute("periodOperate",(List<MemberOperate>)sums.get(2));
        return "searchPeriodOperate";
    }
    //查询一段时间内的上课数页面\
    @GetMapping("/searchPeriod")
    public String searchPeriod()
    {
        return "searchPeriodOperate";
    }

    //下载用户剩余课程的excel
    @GetMapping("/exportExcel")
    public void excelDownload(HttpServletResponse response) throws IOException {
        //准备将Excel的输出流通过response输出到页面下载
        //八进制输出流
        response.setContentType("application/octet-stream");

        //这后面可以设置导出Excel的名称，此例中名为student.xls
        response.setHeader("Content-disposition", "attachment;filename=employee.xls");

        //刷新缓冲
        response.flushBuffer();

        //workbook将Excel写入到response的输出流中，供页面下载
        exportExcelUtils.createExcel().write(response.getOutputStream());
    }
}
